#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @Time   :2021/12/13
# @Author :CJX
# @File   :PH_controller.py
import serial
import time
import binascii
import struct


def PH_choice(choice):
    instructions = 0
    str1 = 0
    a1 = 0
    s = serial.Serial("/dev/ttyS0", 9600)
    if choice == 4:
        instructions = "010600F000014839"
    elif choice == 9:
        instructions = "010600F000020838"
    while a1 <= 1:
        hex_str = bytes.fromhex(instructions)
        s.write(hex_str)
        n = s.inWaiting()
        a1 = a1 + 1
        if n:
            str1 = binascii.b2a_hex(s.read(n))
        time.sleep(1)
    s.flushInput()
    return str1


def PH_controller(choice):
    instructions = 0
    str1 = 0
    a1 = 0
    s = serial.Serial("/dev/ttyS0", 9600)
    if choice == 4:
        instructions = "010600F0000609FB"
    elif choice == 6:
        instructions = "010600F0000549FA"
    elif choice == 9:
        instructions = "010600F0000609FB"
    while a1 <= 1:
        hex_str = bytes.fromhex(instructions)
        s.write(hex_str)
        n = s.inWaiting()
        a1 = a1 + 1
        if n:
            str1 = binascii.b2a_hex(s.read(n))
        time.sleep(1)
    s.flushInput()
    return str1


def PH_status(choice):
    instructions = 0
    str1 = 0
    a1 = 0
    s = serial.Serial("/dev/ttyS0", 9600)
    Inquiry = "010300F10001D5F9"
    while a1 <= 1:
        hex_str = bytes.fromhex(Inquiry)
        s.write(hex_str)
        n = s.inWaiting()
        a1 = a1 + 1
        if n:
            str1 = binascii.b2a_hex(s.read(n))
        time.sleep(1)
    s.flushInput()
    str1 = bytes.decode(str1)
    if choice == 4 and str1 == "01030200023985":
        instructions = "44"
    elif choice == 6 and str1 == "01030200017984":
        instructions = "66"
    elif choice == 9 and str1 == "0103020003f845":
        instructions = "99"
    elif str1 == "0103020004b987":
        instructions = "FF"
    elif str1 == "01030200057847":
        instructions = "11"
    return instructions
